/*
 * Class ActionButtons
 *
 * Version 1.0
 *
 * Monday, August 18, 2008
 *
 * Created by Palidino76
 */

package palidino76.rs2.io.packets;

import palidino76.rs2.Engine;
import palidino76.rs2.players.Player;
import palidino76.rs2.util.Misc;

public class ActionButtons implements Packet {
    /**
     * Handles buttons on interfaces.
     * @param p The Player which the frame should be handled for.
     * @param packetId The packet id this belongs to.
     * @param packetSize The amount of bytes being recieved for this packet.
     */
    public void handlePacket(Player p, int packetId, int packetSize) {
        if (p == null || p.stream == null) {
            return;
        }
        int interfaceId = p.stream.readUnsignedWord();
        int buttonId = p.stream.readUnsignedWord();
        int buttonId2 = 0;
        if (packetId == 233 || packetId == 21 || packetId == 169 || packetId == 232) {
            buttonId2 = p.stream.readUnsignedWord();
        }
        if (buttonId2 == 65535) {
            buttonId2 = 0;
        }
        switch (interfaceId) {
        case 387:
            /*
             * Equipment tab.
             */
            if (buttonId == 55) {
                p.frames.showInterface(p, 667);
                p.frames.setInventory(p, 670);
                p.frames.setItems(p, -1, 64209, 93, p.items, p.itemsN);
                p.frames.setItems(p, -1, 64208, 94, p.equipment, p.equipmentN);
                p.setEquipmentBonus();
            }
            break;
        case 750:
            /*
             * Running button next to minimap.
             */
        case 261:
            /*
             * Settings tab.
             */
            if (buttonId == 1) {
                if (!p.isRunning) {
                    p.isRunning = true;
                    p.frames.setConfig(p, 173, 1);
                } else {
                    p.isRunning = false;
                    p.frames.setConfig(p, 173, 0);
                }
            } else if (buttonId == 14) {
                p.frames.showInterface(p, 742);
            } else if (buttonId == 16) {
                p.frames.showInterface(p, 743);
            }
            break;
        case 464:
            /*
             * Emote tab.
             */
            if (buttonId == 2) // Yes
                p.requestAnim(855, 0);
            else if (buttonId == 3) // No
                p.requestAnim(856, 0);
            else if (buttonId == 4) // Bow
                p.requestAnim(858, 0);
            else if (buttonId == 5) // Angry
                p.requestAnim(859, 0);
            else if (buttonId == 6)  // Think
                p.requestAnim(857, 0);
            else if (buttonId == 7) // Wave
                p.requestAnim(863, 0);
            else if (buttonId == 8) // Shrug
                p.requestAnim(2113, 0);
            else if (buttonId == 9) // Cheer
                p.requestAnim(862, 0);
            else if (buttonId == 10) // Beckon
                p.requestAnim(864, 0);
            else if (buttonId == 11) // Laugh
                p.requestAnim(861, 0);
            else if (buttonId == 12) // Joy jump
                p.requestAnim(2109, 0);
            else if (buttonId == 13) // Yawn
                p.requestAnim(2111, 0);
            else if (buttonId == 14) // Dance
                p.requestAnim(866, 0);
            else if (buttonId == 15) // Jig
                p.requestAnim(2106, 0);
            else if (buttonId == 16) // Spin
                p.requestAnim(2107, 0);
            else if (buttonId == 17) // Headbang
                p.requestAnim(2108, 0);
            else if (buttonId == 18) // Cry
                p.requestAnim(860, 0);
            else if (buttonId == 19) { // Blow kiss
                p.requestGFX(574, 0);
                p.requestAnim(0x558, 0);
            } else if (buttonId == 20) // Panic
                p.requestAnim(2105, 0);
            else if (buttonId == 21) // Raspberry
                p.requestAnim(2110, 0);
            else if (buttonId == 22) // Clap
                p.requestAnim(865, 0);
            else if (buttonId == 23) // Salute
                p.requestAnim(2112, 0);
            else if (buttonId == 24) // Goblin bow
                p.requestAnim(0x84F, 0);
            else if (buttonId == 25) // Goblin salute
                p.requestAnim(0x850, 0);
            else if (buttonId == 26) // Glass box
                p.requestAnim(1131, 0);
            else if (buttonId == 27) // Climb rope
                p.requestAnim(1130, 0);
            else if (buttonId == 28) // Lean
                p.requestAnim(1129, 0);
            else if (buttonId == 29) // Glass wall
                p.requestAnim(1128, 0);
            else if (buttonId == 30) // Head slap
                p.requestAnim(4275, 0);
            else if (buttonId == 31) // Stomp
                p.requestAnim(1745, 0);
            else if (buttonId == 32) // Flap
                p.requestAnim(4280, 0);
            else if (buttonId == 33) // Idea
                p.requestAnim(4276, 0);
            else if (buttonId == 34) // Zombie walk
                p.requestAnim(3544, 0);
            else if (buttonId == 35) // Dombie dance
                p.requestAnim(3543, 0);
            else if (buttonId == 36) { // Zombie hand grab
                p.requestGFX(1244, 0);
                p.requestAnim(7272, 0);
            } else if (buttonId == 37) // Scared
                p.requestAnim(2836, 0);
            else if (buttonId == 38) // Rabbit hop
                p.requestAnim(6111, 0);
            else if (buttonId == 39) { // Skillcape emotes
            }
            else if (buttonId == 40) // Snowman dance
                p.requestAnim(7531, 0); 
            else if (buttonId == 41) { // Air guitar
                p.requestAnim(2414, 0);
                p.requestGFX(1537, 0);
            } else if (buttonId == 42)  { // Safety first
                p.requestAnim(8770, 0); 
                p.requestGFX(1553, 0);
            } else if (buttonId == 43)  { // Explore
                p.requestAnim(9990, 0); 
                p.requestGFX(1734, 0);
            }
        break;
	  case 75:  //Dragon baxe special
		if(buttonId == 10 && !p.usingSpecial && p.equipment[3] == 1377 && p.specialAmount >= 100) {
		    p.specialAmount -= 100;
		    p.specialAmountUpdateReq = true;
		    p.requestAnim(1056, 0);
	    	    p.requestGFX(246, 0);
	  	    p.skillLvl[2] = ((p.getLevelForXP(3)/5)+p.skillLvl[2]);
		    if (p.skillLvl[2] >= 125) {
		    	p.skillLvl[2] = 125;
		    }
		    p.chatText = "Aarrrgggghhhh!";
      	    p.chatTextUpdateReq = true;
        	    p.updateReq = true;
        	    p.frames.setSkillLvl(p, 2);
		} else if(buttonId == 10 && !p.usingSpecial && p.equipment[3] == 1377 && p.specialAmount <= 99) {
		    p.frames.sendMessage(p, "You don't have enough special energy.");
		}
		if(buttonId == 26 && p.autoRetaliate == 0) {
		    p.autoRetaliate = 1;
		    p.frames.setConfig(p, 172, 1);
		} else if(buttonId == 26 && p.autoRetaliate == 1) {
		    p.autoRetaliate = 0;
		    p.frames.setConfig(p, 172, 0);
		}
	  break;
	  case 76:
	  case 77:
	  case 79:
	  case 84: 
	  case 85:
	  case 91:
	  case 92: 
	  case 93:
		if(buttonId == 2) {
		p.attackStyle = 0;
		}
		if(buttonId == 3) {
		p.attackStyle = 1;
		}
		if(buttonId == 4) {
		p.attackStyle = 2;
		}
		if(buttonId == 8 && !p.usingSpecial) {
		    p.usingSpecial = true;
		    p.frames.setConfig(p, 301, 1);
		} else if (buttonId == 8 && p.usingSpecial) {
		    p.usingSpecial = false;
		    p.frames.setConfig(p, 301, 0);
		}
		if(buttonId == 24 && p.autoRetaliate == 0) {
		    p.autoRetaliate = 1;
		    p.frames.setConfig(p, 172, 1);
		} else if(buttonId == 24 && p.autoRetaliate == 1) {
		    p.autoRetaliate = 0;
		    p.frames.setConfig(p, 172, 0);
		}
	  break;
	  case 78:
	  case 81: 
	  case 82:
	  case 83:
	  case 86:
	  case 87:
	  case 88:  
	  case 89:  
		if(buttonId == 2) {
		p.attackStyle = 0;
		}
		if(buttonId == 3) {
		p.attackStyle = 1;
		}
		if(buttonId == 4) {
		p.attackStyle = 2;
		}
		if(buttonId == 5) {
		p.attackStyle = 3;
		}
		if(buttonId == 10 && !p.usingSpecial) {
		    p.usingSpecial = true;
		    p.frames.setConfig(p, 301, 1);
		} else if (buttonId == 10 && p.usingSpecial) {
		    p.usingSpecial = false;
		    p.frames.setConfig(p, 301, 0);
		}
		if(buttonId == 26 && p.autoRetaliate == 0) {
		    p.autoRetaliate = 1;
		    p.frames.setConfig(p, 172, 1);
		} else if(buttonId == 26 && p.autoRetaliate == 1) {
		    p.autoRetaliate = 0;
		    p.frames.setConfig(p, 172, 0);
		}
	  break;
	  case 90: // mage casting staff
		if(buttonId == 1) {
		p.attackStyle = 0;
		}
		if(buttonId == 2) {
		p.attackStyle = 1;
		}
		if(buttonId == 3) {
		p.attackStyle = 2;
		}
		if(buttonId == 9 && p.autoRetaliate == 0) {
		    p.autoRetaliate = 1;
		    p.frames.setConfig(p, 172, 1);
		} else if(buttonId == 9 && p.autoRetaliate == 1) {
		    p.autoRetaliate = 0;
		    p.frames.setConfig(p, 172, 0);
		}
	  break;
	  case 378:
		if (buttonId == 140) {
		    p.frames.setWindowPane(p, 548);
		}
	  break;
        case 182:
            /*
             * Logout interface.
             */
            p.frames.logout(p);
            break;
        case 548:
            /*
             * Main interface.
             */
            break;
        case 763:
            /*
             * Inventory interface with banking.
             */
            if (p.interfaceId != 762) {
                return;
            }
            if (buttonId == 0) {
                if (packetId == 233)
                    Engine.playerBank.bankItem(p, buttonId2, 1);
                else if (packetId == 21)
                    Engine.playerBank.bankItem(p, buttonId2, 5);
                else if (packetId == 169)
                    Engine.playerBank.bankItem(p, buttonId2, 10);
                else if (packetId == 232)
                    Engine.playerBank.bankItem(p, buttonId2, -100);
            }
            break;
        case 762:
            /*
             * Bank interface.
             */
            if (p.interfaceId != 762) {
                return;
            }
            if (buttonId == 73) {
                if (packetId == 233)
                    Engine.playerBank.bankWithdraw(p, buttonId2, 1);
                else if (packetId == 21)
                    Engine.playerBank.bankWithdraw(p, buttonId2, 5);
                else if (packetId == 169)
                    Engine.playerBank.bankWithdraw(p, buttonId2, 10);
                else if (packetId == 232)
                    Engine.playerBank.bankWithdraw(p, buttonId2, -100);
            }
            break;
        default:
            Misc.println("[" + p.username + "] Unhandled button: " + interfaceId + ", " + buttonId + ":" + buttonId2);
            break;
        }
    }
}
